/**
 * 
 */
package com.vector.ding.web.controller.usermgr;

import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.vector.ding.commons.ActionResult;
import com.vector.ding.commons.Constant;
import com.vector.ding.commons.util.StringUtil;
import com.vector.ding.commons.util.front.BuiUtil;
import com.vector.ding.context.AuthContext;
import com.vector.ding.context.WebContext;
import com.vector.ding.dao.bean.MenuEntity;
import com.vector.ding.dao.mybatis.model.User;
import com.vector.ding.web.service.UserService;

/**
 * @author vector
 *
 */
@Controller
@RequestMapping("usermgr")
public class UserMgrController {
	
	@Autowired
	private UserService userService;

	@RequestMapping(value="mgrpage.do", method=RequestMethod.GET)
	public ModelAndView getUserMgrPage() {
		//return new ModelAndView("/usermgr/usermgr");
		ModelAndView mv = new ModelAndView("/usermgr/user_manager");
		mv.addObject("isAdmin", AuthContext.isAdmin());
		return mv;
	}
	
	@RequestMapping(value = "userAuthSetting.do", method = RequestMethod.GET)
	public ModelAndView getUserAuthSettingPage() {
		return new ModelAndView("/usermgr/user_auth_setting");
	}
	
	@ResponseBody
	@RequestMapping(value="getUsers.do", method=RequestMethod.POST)
	public JSONObject getUsers(HttpServletRequest request) {
		String pageIndexString = StringUtil.getString(request.getParameter("pageIndex"));
		String pageSizeString = StringUtil.getString(request.getParameter("start"));
		int pageIndex = pageIndexString.isEmpty() ? Constant.GRID_PAGE_START : Integer.parseInt(pageIndexString);
		int pageSize = pageIndexString.isEmpty() ? Constant.GRID_PAGE_SIZE : Integer.parseInt(pageSizeString);
		List<User> users = userService.getUsers(pageIndex + 1, pageSize, request);
		return BuiUtil.gridAdapter(users);
	}
	
	@ResponseBody
	@RequestMapping(value="editUser.do", method = RequestMethod.POST)
	public JSONObject editUser(String action, HttpServletRequest request) {
		//if ()
		//System.out.println(action);
		boolean operationResult = true;
		try {
			if (Constant.RECORD_ADD.equals(action)) {
				User user = userService.parseUser(request);
				operationResult = userService.addUser(user);
			} else if (Constant.RECORD_UPDATE.equals(action)) {
				User user = userService.parseUser(request);
				operationResult = userService.updateUser(user);
			} else if (Constant.RECORD_DELETE.equals(action)) {
				String[] userIds = request.getParameterValues("ids[]");
				//JSONArray ids = JSONArray.fromObject(userIds);
				if (null == userIds)
					return ActionResult.success();
				//Object[] idArray = ids.toArray();
				userService.deleteUser(userIds);
			}
			
			if (operationResult)
				return ActionResult.success();
		} catch (Exception e) {
			e.printStackTrace();
			return ActionResult.fail("操作失败，请稍后重试！");
		}
/*		
		if (operationResult)
			return ActionResult.success(); */
		return ActionResult.fail("操作失败，请稍后重试！");
	}
	
	@ResponseBody
	@RequestMapping(value="getUserMenu.do", method=RequestMethod.GET)
	public JSONArray getUserMenuTree(String userId, HttpServletRequest request) {
		List<MenuEntity> menuEntities = WebContext.getMenuEntities();
		User user = userService.getUser(userId);
		if (user.getName().equals("admin"))
			return BuiUtil.menuTreeAdapter(menuEntities, null);
		/*if (AuthContext.isAdmin())
			return BuiUtil.menuTreeAdapter(menuEntities, null);*/
		Set<String> userMenuSet = userService.getUserMenuSet(userId);
		return BuiUtil.menuTreeAdapter(menuEntities, userMenuSet);
	}
	
	@ResponseBody
	@RequestMapping(value="saveUserMenu.do", method=RequestMethod.POST)
	public JSONObject getUserMenuTree(String userId, String menus, HttpServletRequest request) {
		userService.deletUserMenuAuth(userId);
		if (StringUtil.isEmpty(menus))
			return ActionResult.success();
		
		String[] menuArray = menus.split(",");
		return userService.saveMenuAuth(userId, menuArray);
	}
}
